﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using EPiBoost.Core.Interfaces.ContentInterfaces;
using EPiServer.Core;
using EPiServer.PlugIn;

namespace EPiBoost.Core.Plugins.Events
{
	public class RenameNewContentOnSave : PlugInAttribute
	{
		public static void Start()
		{
			if (Configuration.Core.Plugins.RenameNewContentOnSave.Enabled)
			{
				EPiServer.DataFactory.Instance.SavingContent += Instance_SavingContent;
			}
		}

		static void Instance_SavingContent(object sender, EPiServer.ContentEventArgs e)
		{
			if (e.RequiredAccess == EPiServer.Security.AccessLevel.NoAccess)
				return; 

			if(e.Content is BlockData || e.Content is PageData)
			{
				UpdateBlockName(e.Content);
			}
		}

		private static void UpdateBlockName(IContent content)
		{
			if(content.Name=="New block" || content.Name == "New page")
			{
				if(content is IMainTitle)
				{
					content.Name = (content as IMainTitle).MainTitle;
				}
			}
			
		}

	}
}
